package Stack;

import java.util.Stack;

public class SolutionEvalRPN {//逆波兰表达式====>将中缀表达式转换成计算机能读懂的后缀表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s:tokens) {
            if(!(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))){
                stack.push(Integer.parseInt(s));
            }else{
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                       stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

}
